/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.inpatient.service.impl;

import me.zhengjie.modules.his.domain.Register;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.his.inpatient.repository.RegisterRepository;
import me.zhengjie.modules.his.inpatient.service.RegisterService;
import me.zhengjie.modules.his.inpatient.service.dto.RegisterDto;
import me.zhengjie.modules.his.inpatient.service.dto.RegisterQueryCriteria;
import me.zhengjie.modules.his.inpatient.service.mapstruct.RegisterMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author his
* @date 2022-11-11
**/
@Service
@RequiredArgsConstructor
public class RegisterServiceImpl implements RegisterService {

    private final RegisterRepository registerRepository;
    private final RegisterMapper registerMapper;

    @Override
    public Map<String,Object> queryAll(RegisterQueryCriteria criteria, Pageable pageable){
        Page<Register> page = registerRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(registerMapper::toDto));
    }

    @Override
    public List<RegisterDto> queryAll(RegisterQueryCriteria criteria){
        return registerMapper.toDto(registerRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RegisterDto findById(Integer registerId) {
        Register register = registerRepository.findById(registerId).orElseGet(Register::new);
        ValidationUtil.isNull(register.getRegisterId(),"Register","registerId",registerId);
        return registerMapper.toDto(register);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RegisterDto create(Register resources) {
        return registerMapper.toDto(registerRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Register resources) {
        Register register = registerRepository.findById(resources.getRegisterId()).orElseGet(Register::new);
        ValidationUtil.isNull( register.getRegisterId(),"Register","id",resources.getRegisterId());
        register.copy(resources);
        registerRepository.save(register);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer registerId : ids) {
            registerRepository.deleteById(registerId);
        }
    }

    @Override
    public void download(List<RegisterDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RegisterDto register : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("患者名字", register.getUserName());
            map.put("年龄", register.getAge());
            map.put("性别", register.getSex());
            map.put("科室", register.getDepartment());
            map.put("医生", register.getDoctor());
            map.put("入院诊断", register.getDiagnose());
            map.put("地址", register.getAddress());
            map.put("电话", register.getPhone());
            map.put("身份证号", register.getIdCard());
            map.put("入院日期", register.getRegisterDate());
            map.put("床号", register.getBedNum());
            map.put("操作员", register.getOperator());
            map.put("预交金", register.getMoney());
            map.put("状态", register.getState());
            map.put("出院日期", register.getEndDate());
            map.put("费用", register.getPrice());
            map.put("折扣", register.getDiscount());
            map.put(" zhuan",  register.getZhuan());
            map.put("创建者", register.getCreateBy());
            map.put("更新者", register.getUpdateBy());
            map.put("创建日期", register.getCreateTime());
            map.put("更新时间", register.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}